<?php
/**
 * Glocation
 * 
 * @author bgfx63
 * @version 
 */
require_once 'Zend/Db/Table/Abstract.php';
require_once 'application/models/User.php';
class Glocation extends Zend_Db_Table_Abstract
{
	protected $_primary = 'id';
    protected $_dependentTables=array('friends');
    
	protected $_referenceMap = array (
								'User' => array (
								'columns' => array('userID'),
								'refTableClass' => 'User',
	 							'refColumns'  => array('id')   
								));
                 
   		
    /**
     * The default table name 
     */
    protected $_name = 'glocation';
    
    function getLocationByUsername($user_name)
    {
    	$users = new User();
		$glocations = new Glocation();
		$robAllen = $users->fetchRow('user_name = "'.$user_name.'"');
		$locationrow = $robAllen->findDependentRowset('Glocation');	
		return $locationrow;
    }
    //getLocationByUserID
    
    function getLocationByUserID($user_id)
    {
    	$users = new User();
		$glocations = new Glocation();
		$robAllen = $users->fetchRow('id = "'.$user_id.'"');
		$locationrow = $robAllen->findDependentRowset('Glocation');	
		return $locationrow;
    }    
    function insertNewLocation($user_id,$lantitude,$longtitude)
    {
    	
		$glocations = new Glocation();
		$newglocations = $glocations->fetchNew();
		$newglocations->user_id = $user_id;
		$newglocations->lantitude = $lantitude;
		$newglocations->longtitude = $longtitude;
		$id = $newglocations->save();
		
		return $id;
    }  
    function insertNewLocationByPhone($phoneNumber,$latitude,$longtitude)
    {
    	$users = new User();
    	$user = $users->getUserByPhoneNumber($phoneNumber);
    	
		$glocations = new Glocation();
		
		$newglocations = $glocations->fetchNew();
		$newglocations->userID = -1;
		if (isset($user)) {
			$newglocations->userID = $user->id;
		}
		$newglocations->phoneNumber = $phoneNumber;
		$newglocations->latitude = $latitude;
		$newglocations->longtitude = $longtitude;
		$id = $newglocations->save();
		
		return $id;
    }  
}
